<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学生"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="异动类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择异动类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否恢复" prop="isRecover">
        <el-select v-model="queryParams.isRecover" placeholder="请选择是否恢复" clearable size="small">
          <el-option
            v-for="dict in isRecoverOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:studentChange:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:studentChange:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentChangeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年级" align="center" prop="year" />
      <el-table-column label="学生" align="center" prop="name" />
      <el-table-column label="原班级" align="center" prop="className" />
      <el-table-column label="异动类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="新班级" align="center" prop="newClassName" />
      <el-table-column label="是否恢复" align="center" prop="isRecover" :formatter="isRecoverFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isRecover != 'Y'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:studentChange:edit']"
          >恢复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-opportunity"
            @click="handleView(scope.row)"
            v-hasPermi="['student:studentChange:edit']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加学籍异动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="异动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择异动类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原班级" prop="classId">
          <el-select v-model="form.classId" clearable placeholder="请选择班级"
              filterable
              remote
              reserve-keyword
              :remote-method="remoteFromClass"
               @change="remoteStudent">
            <el-option
              v-for="item in fromClazzList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生" prop="batchs" >
          <el-select v-model="form.batchs" multiple clearable placeholder="请选择学生">
            <el-option
              v-for="item in studentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.type == '1'" label="新班级" prop="newClass">
          <el-select v-model="form.newClass" clearable placeholder="请选择新班级" 
              filterable
              remote
              reserve-keyword
              :remote-method="remoteToClass">
            <el-option
              v-for="item in toClazzList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原因说明" prop="reason">
          <el-input v-model="form.reason" type="textarea" autosize placeholder="请输入原因说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 恢复学籍对话框 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules2" label-width="80px">
        <el-form-item label="异动类型" prop="type">
          <el-input :value="typeFormat({type:form.type})" disabled/>
        </el-form-item>
        <el-form-item label="原班级" prop="className">
          <el-input v-model="form.className" disabled/>
        </el-form-item>
        <el-form-item label="学生" prop="name" >
          <el-input v-model="form.name" disabled/>
        </el-form-item>
        <el-form-item  label="新班级" prop="newClass">
          <el-select v-model="form.newClass" clearable placeholder="请选择新班级"
              filterable
              remote
              reserve-keyword
              :remote-method="remoteToClass">
            <el-option
              v-for="item in toClazzList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原因说明" prop="reason">
          <el-input v-model="form.reason" type="textarea" autosize placeholder="请输入原因说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="异动类型" prop="type">
          <el-input :value="typeFormat({type:form.type})" disabled/>
        </el-form-item>
        <el-form-item label="原班级" prop="classId">
          <el-input v-model="form.className" disabled/>
        </el-form-item>
        <el-form-item label="学生" prop="batchs" >
          <el-input v-model="form.name" disabled/>
        </el-form-item>
        <el-form-item v-if="form.isRecover == 'Y'" label="新班级" prop="newClass">
          <el-input v-model="form.newClassName" disabled/>
        </el-form-item>
        <el-form-item label="原因说明" prop="reason">
          <el-input v-model="form.reason" type="textarea" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectStudent} from "@/api/base/student";
import { selectClazz } from "@/api/base/clazz";
import { listStudentChange, getStudentChange, delStudentChange, addStudentChange, updateStudentChange, exportStudentChange } from "@/api/student/studentChange";
import { selectSpecialty} from "@/api/base/specialty";
import { getToken } from "@/utils/auth";

export default {
  name: "StudentChange",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专业表格数据
      specialtyList: [],
      // 班级表格数据
      fromClazzList: [],
      // 班级表格数据
      toClazzList: [],
      // 学生表格数据
      studentList: [],
      // 学籍异动表格数据
      studentChangeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openEdit: false,
      openView: false,
      // 异动类型字典
      typeOptions: [],
      // 是否恢复字典
      isRecoverOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        classId: null,
        type: null,
        newClass: null,
        isRecover: null
      },
      // 表单参数
      form: {},
      shutForm: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        batchs: [
          { required: true, message: "学生不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "原班级不能为空", trigger: "blur" }
        ],
      },
      // 表单校验
      rules2: {
        newClass: [
          { required: true, message: "原班级不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("change_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isRecoverOptions = response.data;
    });
  },
  methods: {
    /** 查询学籍异动列表 */
    getList() {
      this.loading = true;
      listStudentChange(this.queryParams).then(response => {
        this.studentChangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteFromClass(query) {
      if (query !== '') {
          this.loading = true;
          selectClazz({title:query}).then(response => {
            this.fromClazzList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
      } else {
         this.loading = true;
         selectClazz({status:'1'}).then(response => {
           this.fromClazzList = response.rows;
           this.total = response.total;
           this.loading = false;
         });
      }
    },
    remoteToClass(query) {
      if (query !== '') {
          this.loading = true;
          selectClazz({title:query}).then(response => {
            this.toClazzList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
      } else {
         this.loading = true;
         selectClazz({status:'1'}).then(response => {
           this.toClazzList = response.rows;
           this.total = response.total;
           this.loading = false;
         });
      }
    },
    remoteStudent() {
      this.loading = true;
      selectStudent({classId:this.form.classId, status: '1'}).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 异动类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 是否恢复字典翻译
    isRecoverFormat(row, column) {
      return this.selectDictLabel(this.isRecoverOptions, row.isRecover);
    },
    // 取消按钮
    cancel() {
        this.open = false;
        this.openEdit = false;
        this.openView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        batchs: null,
        stuId: null,
        classId: null,
        type: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        newClass: null,
        isRecover: "N",
        reason: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetShut() {
      this.shutForm = {
        year: null,
        fromSpecId: null,
        fromClassId: null,
        toSpecId: null,
        toClassId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.openEdit = false;
      this.openView = false;
      this.title = "添加学籍异动";
    },
    /** 恢复按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudentChange(id).then(response => {
        this.form = response.data;
        this.open = false;
        this.openEdit = true;
        this.openView = false;
        this.title = "恢复学籍";
        this.remoteFromClass(response.data.className);
        this.remoteToClass(response.data.newClassName);
        //this.remoteStudent();
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getStudentChange(id).then(response => {
        this.form = response.data;
        this.open = false;
        this.openEdit = false;
        this.openView = true;
        this.title = "恢复学籍";
        this.remoteFromClass(response.data.className);
        this.remoteToClass(response.data.newClassName);
        //this.remoteStudent(response.data.name);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudentChange(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.openEdit = false;
              this.openView = false;
              this.getList();
            });
          } else {
            addStudentChange(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.openEdit = false;
              this.openView = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除学籍异动编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudentChange(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学籍异动数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStudentChange(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
